Import Specific Image
I=imread('0101_baseline_anterior copy 6.jpg');
imshow(I)
Crop & Convert to Blackwhite
J=I;
%J=imcrop(I,[0.5 40.5 613 397]);
J=rgb2gray(J);
imshow(J)
Loop to reverse-shade shaded regions (should also test for if regions exist and are equivelent to the ones defined here...)
x1=[6, 149, 6, 556, 556];
x2=[301, 174, 141, 635, 635];
y1=[450, 6, 6, 441, 6];
y2=[474, 34, 38, 475, 40];
for k=1:length(x1)
if k==1 || k==2 || k==3
s=2;
else s=1;
end
for i = (x1(k)+2):(x2(k)-2)
psi{k}(i,1)=mean(J((y1(k)-1-s):(y1(k)-1),i));
psi{k}(i,2)=mean(J((y2(k)+1):(y2(k)+1+s),i));
psi{k}(i,3)=mean(J((y1(k)):(y1(k)+s),i));
psi{k}(i,4)=mean(J((y2(k)-s):(y2(k)),i));
for j = y1(k):y2(k)
J(j,i)=J(j,i)*((psi{k}(i,1)/psi{k}(i,3))*((y2(k)-j)/(y2(k)-y1(k)))+...
(psi{k}(i,2)/psi{k}(i,4))*((j-y1(k))/(y2(k)-y1(k))));
end
end
for i = [(x1(k)+1),(x2(k)-1)]
psi{k}(i,1)=mean(J((y1(k)-1-s):(y1(k)-1),i));
psi{k}(i,2)=mean(J((y2(k)+1):(y2(k)+1+s),i));
psi{k}(i,3)=mean(J((y1(k)+1):(y1(k)+1+s),i));
psi{k}(i,4)=mean(J((y2(k)-1-s):(y2(k)-1),i));
for j = (y1(k)+1):(y2(k)-1)
J(j,i)=J(j,i)*((psi{k}(i,1)/psi{k}(i,3))*((y2(k)-j)/(y2(k)-y1(k)))+...
(psi{k}(i,2)/psi{k}(i,4))*((j-y1(k))/(y2(k)-y1(k))));
end
end
for i = [(x1(k)),(x2(k))]
psi{k}(i,1)=mean(J((y1(k)-1-s):(y1(k)-1),i));
psi{k}(i,2)=mean(J((y2(k)+1):(y2(k)+1+s),i));
psi{k}(i,3)=mean(J((y1(k)+2):(y1(k)+2+s),i));
psi{k}(i,4)=mean(J((y2(k)-2-s):(y2(k)-2),i));
for j = (y1(k)+2):(y2(k)-2)
J(j,i)=J(j,i)*((psi{k}(i,1)/psi{k}(i,3))*((y2(k)-j)/(y2(k)-y1(k)))+...
(psi{k}(i,2)/psi{k}(i,4))*((j-y1(k))/(y2(k)-y1(k))));
end
end
end
imshow(J)
Find area of text & connect nearby regions.
J_test=J>254;
J_test(35:450,1:590)=0;
cc = bwconncomp(J_test);
L = labelmatrix(cc);
rgb = label2rgb(L, 'jet', [.7 .7 .7], 'shuffle');
imshow(rgb)
bw2 = bwdist(J_test) <= 25;
imshow(bw2)
L2 = labelmatrix(bwconncomp(bw2));
rgb2 = label2rgb(L2, 'jet', [.7 .7 .7], 'shuffle');
imshow(rgb2)
L2(~J_test) = 0;
imshow(label2rgb(L2, 'jet', [.7 .7 .7], 'shuffle'))
Remove text by dilating the just text, then inward interpolation
L3=imbinarize(L2*255);
se = strel('rectangle',[4 4]);
dilatedL3 = imdilate(L3,se);
dilatedL3 = imbinarize(dilatedL3*255);
imshow(dilatedL3)
JJ = immultiply(J,~dilatedL3);
JJ = double(JJ);
JJ(JJ==0)=NaN;
JJ=inpaint_nans(JJ,5);
JJ=uint8(JJ);
imshow(JJ)
Remove verticle bar on the right of the image
L4=zeros(480,640);
L4(42:439,615:636)=1;
JJ = immultiply(JJ,~L4);
JJ = double(JJ);
JJ(JJ==0)=NaN;
JJ=inpaint_nans(JJ);
JJ=uint8(JJ);
imshow(JJ)
centreregion = zeros(480,640);
centreregion(172:280,167:360)=1;
cross = JJ<50;
cross = immultiply(cross, centreregion);
if sum(sum(cross)) >= 10
se = strel('rectangle',[4 4]);
dilatedcross = imdilate(cross,se);
JJ = immultiply(JJ,~dilatedcross);
JJ = double(JJ);
JJ(JJ==0)=NaN;
JJ=inpaint_nans(JJ, 4);
JJ=uint8(JJ);
end
imshow(JJ)
Test if
are present in J, then remove image's background:
First, compute x- & y-Derivatives
[J_Gx,J_Gy] = imgradientxy(JJ);
J_Gx=abs(J_Gx);
imshow(J_Gx)
Perform first Otsu's Test (does
?) to remove background & disply output beside original image
level = graythresh(JJ);
BW = imbinarize(JJ,level);
BW=imfill(BW,'holes');
imshowpair(JJ,BW,'montage')
Test if
, and
, then if true, perform corrective filling-in procedure to properly segment the background:
krn=[1 -1];
dimBW=size(BW);
if ~(mean(mean(J_Gx(1:5,:)))>mean(mean(J_Gx)))...
&& ((sum(sum(~imcrop(BW,[238.5 335.5 114 52])))>0 ...
|| sum(sum(~imcrop(BW,[152.5 121.5 72 72])))>0 ...
||sum(sum(~imcrop(BW,[402.5 141.5 72 72])))>0))
for i = 1:dimBW(1)
changes_first{i}=conv(krn, BW(i,:));
changes1_first{i}=find(changes_first{i}==1 | changes_first{i}==-1);
end
BW(1,changes1_first{1}(1):changes1_first{1}(length(changes1_first{1})-1))=1;
BW(dimBW(1),changes1_first{dimBW(1)}(1):...
changes1_first{dimBW(1)}(length(changes1_first{dimBW(1)})-1))=1;
BW = imfill(BW, 'holes');
imshowpair(JJ,BW,'montage')
end
If
, and
was true, test if previous code corrected the problems:
if ~((sum(sum(~imcrop(BW,[238.5 335.5 114 52])))==0 ...
&& sum(sum(~imcrop(BW,[152.5 121.5 72 72])))==0 ...
&& sum(sum(~imcrop(BW,[402.5 141.5 72 72])))==0))
fprintf('WARNING: Otsu Test Failure - Complete Dependence on Alternative to Otsu');
end
Automatically detect if Otsu's test failed to remove the proper background, and if so apply 2nd method for removing the background [so far dependent on mu & sigma in:
edge(JJ, 'Canny', mu ,sigma) , and dependent on 4 distinct background regions - need to generalize]
if (sum(sum(~imcrop(BW,[238.5 335.5 114 52])))>0 ...
|| sum(sum(~imcrop(BW,[152.5 121.5 72 72])))>0 ...
||sum(sum(~imcrop(BW,[402.5 141.5 72 72])))>0)
BW1 = edge(JJ,'Canny',.7,3);
s = regionprops(BW1,'centroid');
centroids = cat(1, s.Centroid);
imshow(BW1)
hold on
plot(centroids(:,1),centroids(:,2), 'g*')
hold off
s1 = regionprops(BW1,'Extrema');
s3 = regionprops(BW1,'BoundingBox');
s1 = struct2cell(s1);
s3 = struct2cell(s3);
dim=size(BW1);
BW2=BW1;
dim3=size(s3);
if dim3(2)==3 && centroids(2,2)<mean(centroids(:,2))
%gamma1
BW2(floor(s3{1}(2)):dim(1),1)=1;
BW2(dim(1),1:(s3{1}(3)))=1;
%gamma4
BW2(floor(s3{3}(2)):dim(1),dim(2))=1;
BW2(dim(1),floor(s3{3}(1)):dim(2))=1;
%gamma2
BW2(1,floor(s3{2}(1)):(floor(s3{2}(1))+s3{2}(3))+1)=1;
end
if dim3(2)==4 && centroids(2,2)<mean(centroids(:,2))...
&& centroids(3,2)<mean(centroids(:,2))
%gamma1
BW2(floor(s3{1}(2)):dim(1),1)=1;
BW2(dim(1),1:(s3{1}(3)))=1;
%gamma4
BW2(floor(s3{4}(2)):dim(1),dim(2))=1;
BW2(dim(1),floor(s3{4}(1)):dim(2))=1;
%gamma2
BW2(1,floor(s3{2}(1)):(floor(s3{2}(1))+s3{2}(3))+1)=1;
%gamma3
BW2(1,floor(s3{3}(1)):(floor(s3{3}(1))+s3{3}(3))+1)=1;
end
if (dim3(2)==3 && centroids(2,2)<mean(centroids(:,2))...
&& centroids(3,2)<mean(centroids(:,2)))||...
(dim3(2)==4 && centroids(2,2)<mean(centroids(:,2)))
BW2 = imfill(BW2, 'holes');
BW2 = ~BW2;
imshow(BW2)
BW=BW2;
end
if ~(dim3(2)==3 || dim3(2)==4)
fprintf('FATAL WARNING: x=Regions(BW1) not in {3,4}');
end
if dim3(2)==3 && centroids(2,2)>mean(centroids(:,2))
fprintf('FATAL WARNING: x_2=Region_2(BW1) not < mean(Regions(BW1))');
end
if dim3(2)==4 && (centroids(2,2)>mean(centroids(:,2))||...
centroids(3,2)>mean(centroids(:,2)))
fprintf('FATAL WARNING: x_2=Region_2(BW1) or x_3=Region_3(BW1) not < mean(Regions(BW1))');
end
end
Restrict Calculations to the proper subset of the Image:
JJJ=immultiply(JJ,BW);
JJJ(JJJ==0)=NaN;
imshow(JJJ)
dimIJK=size(JJJ);
IJK=imbinarize(JJJ);
imshow(IJK)
Reduce Cropping (x-direction dependent) by 3 pixels
krn=[1 -1];
for i = 1:dimIJK(1)
changes{i}=conv(krn, IJK(i,:));
changes1{i}=find(changes{i}==1 | changes{i}==-1);
end
for i = 1:dimIJK(1)
IJK(i, max(changes1{i}-1,1))=0;
IJK(i, max(changes1{i}-2,1))=0;
IJK(i, max(changes1{i}-3,1))=0;
IJK(i, min(changes1{i}+1,dimIJK(2)))=0;
IJK(i, min(changes1{i}+2,dimIJK(2)))=0;
IJK(i, min(changes1{i}+3,dimIJK(2)))=0;
IJK(i, min(changes1{i}, dimIJK(2)))=0;
end
JJ=immultiply(JJ,uint8(IJK));
JJ(JJ==0 | isnan(JJ))=NaN;
imshow(JJ)
Compute x- & y-Derivatives
[Gx,Gy] = imgradientxy(JJ);
Gy=abs(Gy);
Gx=abs(Gx);
Gy=imgaussfilt(imgaussfilt(Gy));
Gx=imgaussfilt(imgaussfilt(Gx));
imshow(Gx)
Find the most most significant y-derivative areas
First Guess: .3*nanstd(nanstd(double(Gy)))^.5*range(range(Gy))
Gy=Gy+.01*Gx;
IJ=Gy>=max(max(Gy))-.9*floor(range(range(Gy)));
dimIJ=size(IJ);
IJ(1:floor(0.3*dimIJ(1)),1:dimIJ(2))=0;
IJ=bwareafilt(IJ,2);
imshow(IJ)
Find where the two largest and hottest regions
s = regionprops(IJ,'centroid');
centroids = cat(1, s.Centroid);
imshow(IJ)
middle = ([sum(centroids(:,1))/2,sum(centroids(:,2))/2]);
hold on
plot(centroids(:,1),centroids(:,2), 'rs', 'MarkerSize', 15)
plot(middle(1), middle(2),'gx', 'MarkerSize', 15)
hold off
leftregion = imcrop(IJ,[0 0 middle(1) 480]);
rightregion = imcrop(IJ,[middle(1) 0 640 480]);
Fit polynomial line to the left area
ii=find(leftregion);
[yy,xx]=ind2sub(size(IJ),ii);
p1 = polyfit(xx,yy,2);
Right area
ii=find(rightregion);
[yy,xx]=ind2sub(size(IJ),ii);
p2 = polyfit(xx,yy,2);
Plots of polynomial fits & saves it to cd
imshow(JJ)
x1 = 0:614;
y1 = polyval(p1,x1);
x2 = (-middle(1)):(614-middle(1));
y2 = polyval(p2,x2);
hold on
plot(x1,y1)
plot(x2+middle(1),y2)
%saveas(gcf,strcat('poly',num2str(i)), 'jpg')
hold off